package HTTPServer.Methods;

import HTTPServer.*;
import HTTPServer.Server.*;
import HTTPServer.Bank.*;
import java.net.*;
import java.util.*;
import java.io.*;

public class GetMethod implements MethodStrategy {

    private HTTPServerStrategy server;

    public GetMethod(HTTPServerStrategy server) {
        this.server = server;
    }

    public void processRequest(String request) throws IOException {
        String req = request.substring(4, request.length() - 9).trim();
        if (req.indexOf("/.") != -1 || req.endsWith("~")) {
            server.errorReport(server.getPOut(), server.getConnection(), "403", "Forbidden",
                    "You don't have permission to access " +
                    "the requested URL.");
        } else {
            String result = "BAD REQUEST";
            String code = "400";
            String title = "Bad Request";
            String htmlTitle = "Bank Error";

            String[] reqArray = req.split("/");

            if (reqArray.length >= 4 && reqArray[1].equals("Account")) {

                String name = reqArray[2];
                String method = reqArray[3].split("\\?")[0];
                Account account = server.getBank().getAccount(name);

                // /Account/Allan/getBalance
                if (method.equals("getBalance")) {
                    htmlTitle = "Account Balance";
                    result = "Account balance: " + account.getBalance();
                    code = "200";
                    title = "OK";

                // /Account/Allan/deposit?amount=100
                } else if (method.equals("deposit")) {
                    // amount=100 + eventually parameters
                    String parmsArray = reqArray[3].split("\\?")[1];
                    // amount=100
                    String[] parmArray = parmsArray.split("\\&");
                    if (parmArray.length >= 1 && parmArray[0].split("\\=")[0].equals("amount")) {
                        double amount = Double.parseDouble(parmArray[0].split("\\=")[1]);
                        account.deposit(amount);

                        htmlTitle = "Account Deposite";
                        result = "Account deposite: " + amount;
                        code = "200";
                        title = "OK";
                    }
                // /Account/Allan/withdraw?amount=100
                } else if (method.equals("withdraw")) {
                    // amount=100 + eventually parameters
                    String parmsArray = reqArray[3].split("\\?")[1];
                    // amount=100
                    String[] parmArray = parmsArray.split("\\&");
                    if (parmArray.length >= 1 && parmArray[0].split("\\=")[0].equals("amount")) {
                        double amount = Double.parseDouble(parmArray[0].split("\\=")[1]);
                        account.withdraw(amount);

                        htmlTitle = "Account Withdraw";
                        result = "Account withdraw: " + amount;
                        code = "200";
                        title = "OK";
                    }
                }
            }

            //sending result to client
            server.getPOut().print(
                    "HTTP/1.0 " + code + " " + title + "\r\n" +
                    "\r\n" +
                    "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n" +
                    "<TITLE>" + htmlTitle + "</TITLE>\r\n" +
                    "</HEAD>\r\n<BODY>\r\n" +
                    result +
                    "\r\n</BODY></HTML>\r\n");
            server.log(server.getConnection(), code + " " + title);

        }
    }
}
